const fs = require('fs');
const path = require('path');
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

// 上传商品图片
exports.uploadProductImage = async (req, res) => {
  try {
    if (!req.file) {
      return res.status(400).json({
        status: 'error',
        message: '未找到上传的图片'
      });
    }
    
    // 文件相对路径 (相对于public目录)
    const relativePath = `uploads/products/${req.file.filename}`;
    // 完整URL路径
    const fullUrl = `${req.protocol}://${req.get('host')}/api/${relativePath}`;
    
    res.json({
      status: 'success',
      message: '商品图片上传成功',
      data: {
        path: relativePath,
        url: fullUrl,
        filename: req.file.filename,
        originalname: req.file.originalname,
        size: req.file.size
      }
    });
  } catch (error) {
    console.error('上传商品图片失败:', error);
    res.status(500).json({
      status: 'error',
      message: '上传商品图片失败',
      error: error.message
    });
  }
};

// 上传用户头像
exports.uploadAvatar = async (req, res) => {
  try {
    if (!req.file) {
      return res.status(400).json({
        status: 'error',
        message: '未找到上传的图片'
      });
    }
    
    // 文件相对路径 (相对于public目录)
    const relativePath = `uploads/avatars/${req.file.filename}`;
    // 完整URL路径
    const fullUrl = `${req.protocol}://${req.get('host')}/api/${relativePath}`;
    
    res.json({
      status: 'success',
      message: '头像上传成功',
      data: {
        path: relativePath,
        url: fullUrl,
        filename: req.file.filename,
        originalname: req.file.originalname,
        size: req.file.size
      }
    });
  } catch (error) {
    console.error('上传头像失败:', error);
    res.status(500).json({
      status: 'error',
      message: '上传头像失败',
      error: error.message
    });
  }
};

// 上传渠道图标
exports.uploadChannelIcon = async (req, res) => {
  try {
    if (!req.file) {
      return res.status(400).json({
        status: 'error',
        message: '未找到上传的图片'
      });
    }
    
    // 文件相对路径 (相对于public目录)
    const relativePath = `uploads/channels/${req.file.filename}`;
    // 完整URL路径
    const fullUrl = `${req.protocol}://${req.get('host')}/api/${relativePath}`;
    
    res.json({
      status: 'success',
      message: '渠道图标上传成功',
      data: {
        path: relativePath,
        url: fullUrl,
        filename: req.file.filename,
        originalname: req.file.originalname,
        size: req.file.size
      }
    });
  } catch (error) {
    console.error('上传渠道图标失败:', error);
    res.status(500).json({
      status: 'error',
      message: '上传渠道图标失败',
      error: error.message
    });
  }
}; 

// 上传小票二维码
exports.uploadQrCode = async (req, res) => {
  try {
    if (!req.file) {
      return res.status(400).json({
        status: 'error',
        message: '未找到上传的图片'
      });
    }
    
    // 文件相对路径 (相对于public目录)
    const relativePath = `uploads/qrcodes/${req.file.filename}`;
    
    // 使用API路径
    const fileUrl = `/api/${relativePath}`;
    
    // 更新系统设置
    await prisma.systemSetting.upsert({
      where: {
        category_key: {
          category: 'frontend.store',
          key: 'qrCodeUrl'
        }
      },
      update: {
        value: fileUrl,
        updatedAt: new Date()
      },
      create: {
        category: 'frontend.store',
        key: 'qrCodeUrl',
        value: fileUrl,
        description: '小票二维码图片URL',
        updatedAt: new Date()
      }
    });
    
    res.json({
      status: 'success',
      message: '小票二维码上传成功',
      data: {
        path: relativePath,
        url: fileUrl,
        filename: req.file.filename,
        originalname: req.file.originalname,
        size: req.file.size
      }
    });
  } catch (error) {
    console.error('上传小票二维码失败:', error);
    res.status(500).json({
      status: 'error',
      message: '上传小票二维码失败',
      error: error.message
    });
  }
}; 